package jaux.message.codec.utils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jaux.message.codec.ByteMessageEncoder;
import jaux.message.codec.annotation.ByteMessageField;

public class MessageClassUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(MessageClassUtils.class);

	
	public static List<Field> getMessageField(Class msgClz){
		return FieldUtils.getFieldsListWithAnnotation(msgClz, ByteMessageField.class);
	}
	
	public static Object getFieldValue(Object obj, Field field) {
		if(obj == null || field == null || !(field.getDeclaringClass().isInstance(obj) )) {
			return null;
		}
		try {
			return PropertyUtils.getProperty(obj, field.getName());
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			logger.warn("读取对象属性值失败", e);
		}
		return null;
	}
}
